<?php
declare (strict_types=1);

namespace app\admin\validate;

use think\Validate;

class Module extends Validate
{
    protected $rule = [
        'name'   => 'require',
        'code'   => 'require|checkExistCode',
        'status' => ['require', 'in' => '1,2'],
    ];

    protected $message = [
        'name.require' => '请输入模型名称',
        'code.require' => '请输入模型标识',
        'status'       => '请选择正确的状态',
    ];

    protected $scene = [
        'add'  => ['name', 'code', 'status'],
        'edit' => ['name', 'status']
    ];

    public function checkExistCode($value, $rule, $data = [])
    {
        $moduleModel = new \app\common\model\Module([], $data['website_code']);
        $module      = $moduleModel->findModuleByCode($value);
        if (!empty($module)) {
            return '模型标识已存在';
        }

        return true;
    }


}
